package com.swinginwind.owf.leave.serviceImpl;

import java.util.List;
import java.util.Map;

import oracle.apps.fnd.wf.WFContext;
import oracle.apps.fnd.wf.engine.WFEngineAPI;

import com.swinginwind.BaseDao;
import com.swinginwind.owf.WFContextFactory;
import com.swinginwind.owf.leave.Constants;
import com.swinginwind.owf.leave.SqlStatements;
import com.swinginwind.owf.leave.service.LeaveService;
import com.swinginwind.owf.leave.vo.LeaveVO;

public class LeaveServiceImpl implements LeaveService {

	private BaseDao baseDao;
	
	private WFContextFactory contextFactory;
	
	
	@Override
	public List<Map<String, Object>> getProcessList() {
		// TODO Auto-generated method stub
		String sql = SqlStatements.BASE_ITEM_QUERY_SQL;
		return baseDao.excuteQuery(sql);
	}
	
	@Override
	public List<Map<String, Object>> getMyAppProcessList(String employee) {
		// TODO Auto-generated method stub
		String sql = "select * from (" + SqlStatements.BASE_ITEM_QUERY_SQL
				+ ") t10 where t10.employee='" + employee + "'";
		return baseDao.excuteQuery(sql);
	}




	@Override
	public List<Map<String, Object>> getMyToDoProcessList(String manager) {
		// TODO Auto-generated method stub
		String sql = "select * from (" + SqlStatements.BASE_ITEM_QUERY_SQL
				+ ") t10 where t10.manager='" + manager + "'";
		return baseDao.excuteQuery(sql);
	}
	
	
	@Override
	public String startLeaveProcess(LeaveVO leave) {
		// TODO Auto-generated method stub
		WFContext wCtx = contextFactory.getWorkflowContext();
		String itemKey = this.getNextItemKey();
		WFEngineAPI.createProcess(wCtx, Constants.LEAVE_ITEM_TYPE, itemKey, Constants.LEAVE_PROCESS);
		
		WFEngineAPI.setItemAttrText(wCtx, Constants.LEAVE_ITEM_TYPE, itemKey, Constants.LEAVE_ATTR_APP_DATE, leave.getAppDate());
		WFEngineAPI.setItemAttrText(wCtx, Constants.LEAVE_ITEM_TYPE, itemKey, Constants.LEAVE_ATTR_EMPLOYEE, leave.getEmployee());
		WFEngineAPI.setItemAttrText(wCtx, Constants.LEAVE_ITEM_TYPE, itemKey, Constants.LEAVE_ATTR_MANAGER, leave.getManager());
		WFEngineAPI.setItemAttrText(wCtx, Constants.LEAVE_ITEM_TYPE, itemKey, Constants.LEAVE_ATTR_ORDER_ID, leave.getOrderId());
		WFEngineAPI.setItemAttrText(wCtx, Constants.LEAVE_ITEM_TYPE, itemKey, Constants.LEAVE_ATTR_START_DATE, leave.getStartDate());
		WFEngineAPI.setItemAttrText(wCtx, Constants.LEAVE_ITEM_TYPE, itemKey, Constants.LEAVE_ATTR_END_DATE, leave.getEndDate());
		WFEngineAPI.setItemAttrText(wCtx, Constants.LEAVE_ITEM_TYPE, itemKey, Constants.LEAVE_ATTR_STATUS, leave.getStatus());
		WFEngineAPI.setItemAttrText(wCtx, Constants.LEAVE_ITEM_TYPE, itemKey, Constants.LEAVE_ATTR_DESC, leave.getDesc());
		
		WFEngineAPI.startProcess(wCtx, Constants.LEAVE_ITEM_TYPE, itemKey);
		
		return null;
	}
	
	
	
	private String getNextItemKey() 
	{
		return baseDao.excuteQuery(SqlStatements.BASE_ITEM_KEY_SQL).get(0).get("SEQ").toString();
	}
	


	public BaseDao getBaseDao() {
		return baseDao;
	}


	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	public WFContextFactory getContextFactory() {
		return contextFactory;
	}

	public void setContextFactory(WFContextFactory contextFactory) {
		this.contextFactory = contextFactory;
	}

	




	
	
	

}
